#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/mutex.h>
#include <linux/bitops.h>
#include <linux/interrupt.h>
#include <linux/bug.h>			/* For BUG_ON.  */
#include <linux/cpu.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <linux/delay.h>
#include <linux/smp.h>
#include <linux/kernel_stat.h>
#include <linux/sched.h>
#include <linux/percpu-defs.h>
#include <linux/wait.h>
#include <linux/gpio/driver.h>
#include <linux/atomic.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/kfifo.h>
#include <linux/mm.h>
#include <linux/timer.h>
#include <asm-generic/bitops/non-atomic.h>
#include <asm-generic/bitops/find.h>
#include <linux/bitmap.h>
#include <linux/of.h>


/*
                                       /usr/local/arm/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin
                                        *
  sudo  make ARCH=arm64  CROSS_COMPILE=/usr/local/arm/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-  dtbs


step 1:

  sudo  make ARCH=arm64  CROSS_COMPILE=/usr/local/arm/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-

step 2:

  sudo make ARCH=arm64  rk3399-tinker_board_2.img  CROSS_COMPILE=/usr/local/arm/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-   -j8

step 3:

  dd if=boot.img of=/dev/mmcblk1p4


Documentation/rtc.txt

cd  /proc/


 *
 *
 */


#define  TRUE(arg) BUG_ON(!(arg));

#define MY_MAX_MINORS  1
#define   DEVICE_NAME  "device name 500"

DEFINE_KFIFO(myfifo, char, 1024);

struct my_device_data {
	unsigned char *buf;
	struct cdev cdev;
};

DECLARE_WAIT_QUEUE_HEAD(wq);

static struct my_device_data my_dev_data;

static int my_open(struct inode *inode, struct file *file) {
	int ret;
	pr_info("a3 my_open \n");
	ret = kfifo_put(&myfifo, 'e');
	ret = kfifo_put(&myfifo, 'n');
	ret = kfifo_put(&myfifo, 'd');
	ret = kfifo_put(&myfifo, '\n');
	return 0;
}

static ssize_t my_write(struct file *filp, const char __user *user_buffer,
		size_t size, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("write");
	ret = kfifo_from_user(&myfifo, user_buffer, size, &len);
	if (ret != 0) {
		pr_err("kfifo_from_user error");
		return 0;
	}
	if (len <= 0)
		return 0;
	*offset += len;
	wake_up(&wq);
	return len;
}

static ssize_t my_read(struct file *filp, char __user *user_buffer,
		size_t count, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("read");
	ret = kfifo_to_user(&myfifo, user_buffer, count, &len);
	if (len <= 0)
		return 0;
	*offset += len;
	return len;
}

unsigned int my_poll(struct file *flip, struct poll_table_struct *table) {
	int mask = 0;
	pr_info("my_poll \n");
	poll_wait(flip, &wq, table);
	if (kfifo_is_empty(&myfifo)) {

	} else {
		mask |= POLLIN | POLLRDNORM;
	}
	return mask;
}

int my_close(struct inode *inode, struct file *flip) {
	pr_info("my_close \n");
	pr_info("my_dev_data.buf : %s\n", my_dev_data.buf);
	return 0;
}

int my_mmap(struct file *flip, struct vm_area_struct *vma) {
	if (remap_pfn_range(vma, vma->vm_start,
			virt_to_phys(my_dev_data.buf) >> PAGE_SHIFT,
			vma->vm_end - vma->vm_start, vma->vm_page_prot))
		return -EAGAIN;
	return 0;
}

static const struct file_operations my_fops = { .owner = THIS_MODULE, .open =
		my_open, .read = my_read, .write = my_write, .poll = my_poll, .release =
		my_close, .mmap = my_mmap,

};

static dev_t mydev;



static __init int my_init(void) {
	int err;
	int ret;
	const char *str;
	struct device_node *test_device_node;
	u32 out_values[2] = {0};

	struct property *test_node_property;
	pr_info("a3 init_module\n");err = alloc_chrdev_region(&mydev, 0, MY_MAX_MINORS, DEVICE_NAME);
	if (err != 0) {
		return err;
	}

	cdev_init(&my_dev_data.cdev, &my_fops);
	cdev_add(&my_dev_data.cdev, MKDEV(MAJOR(mydev), 0), 1);

//	addr = __get_free_page(GFP_KERNEL);my_dev_data.buf =(unsigned char*)addr;
//	memset(my_dev_data.buf,0,PAGE_SIZE);
//	pr_info("RTC_IRQ %d \n",RTC_IRQ);


	test_device_node = of_find_node_by_path("/vcc100");  //在设备树节点中查找test这个节点
				if(test_device_node == NULL){
					printk("find error!\n");
				} else {
					ret=of_get_child_count(test_device_node);
							printk("adc-keys of_get_child_count = %d\n",ret);
				}

				ret = of_property_read_u32(test_device_node, "pin", out_values);
													if(ret < 0){
														printk("of_property_read_u32 is error!\n");
													} else{
														printk("vcc100.pin = %d\n",out_values[0]);
													}
              ret = of_property_read_string(test_device_node, "name124", &str);
																						if(ret < 0){
																							printk("of_property_read_string is error!\n");
																						}
																						else{
																							printk("vcc100.name124 is %s.\n", str);
																						}





	test_device_node = of_find_node_by_path("/adc-keys");  //在设备树节点中查找test这个节点
			if(test_device_node == NULL){
				printk("find error!\n");
			} else {
				ret=of_get_child_count(test_device_node);
						printk("adc-keys of_get_child_count = %d\n",ret);
			}



	test_device_node = of_find_node_by_path("/vcc-lcd");  //在设备树节点中查找test这个节点
		if(test_device_node == NULL){
			printk("find error!\n");
		} else {
			printk("find ok   !\n");
			ret=of_get_child_count(test_device_node);
			printk("of_get_child_count = %d\n",ret);

			if(of_property_read_bool(test_device_node,"enable-active-high")){
				printk("enable-active-high\n");
			};

			ret = of_property_read_string(test_device_node, "compatible", &str);
									if(ret < 0){
										printk("of_property_read_string is error!\n");
									}
									else{
										printk("compatible is %s.\n", str);
									}

										ret = of_property_read_u32(test_device_node, "startup-delay-us", out_values);
										if(ret < 0){
											printk("of_property_read_u32 is error!\n");
										} else{
											printk("startup-delay-us = %d\n",out_values[0]);
										}

				test_node_property = of_find_property(test_device_node, "compatible", NULL);
				if(test_node_property == NULL){
					printk("of_find_property is error!\n");
				}
				else{
					printk("test_node_property name is %s.\n", test_node_property->name);
					printk("test_node_property value is %s.\n", (char *)test_node_property->value);
				}

				test_node_property = of_find_property(test_device_node, "regulator-name", NULL);
								if(test_node_property == NULL){
									printk("of_find_property is error!\n");
								}
								else{
									printk("test_node_property name is %s.\n", test_node_property->name);
									printk("test_node_property value is %s.\n", (char *)test_node_property->value);
								}

								test_node_property = of_find_property(test_device_node, "startup-delay-us", NULL);
																if(test_node_property == NULL){
																	printk("of_find_property is error!\n");
																}
																else{
																	printk("test_node_property name is %s.\n", test_node_property->name);
//																	printk("test_node_property value is %d.\n", (int*)test_node_property->value);
																}
		}

		test_device_node = of_find_node_by_path("/gpio-keys/button@0");  //在设备树节点中查找test这个节点
				if(test_device_node == NULL){
					printk("find error 1!\n");
				} else {
					printk("find ok 1   !\n");
					ret = of_property_read_u32(test_device_node, "linux,input-type", out_values);
															if(ret < 0){
																printk("of_property_read_u32 is error!\n");
															} else{
																printk("linux,input-type = %d\n",out_values[0]);
															}
				}

				test_device_node = of_find_node_by_path("/&dsi/mipi-panel");  //在设备树节点中查找test这个节点
							if(test_device_node == NULL){
								printk("find error 2!\n");
							} else {
								printk("find ok 2!\n");
							}
	return 0;
}

static void __exit my_exit(void) {
		pr_info("a3 cleanup_module\n");
/* release devs[i] fields */
		cdev_del(&my_dev_data.cdev);
		unregister_chrdev_region(mydev, MY_MAX_MINORS);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");

